<script setup>
import { introApi } from "@/services/index";
import { onLoad, onShow } from "@dcloudio/uni-app";
import { nextTick, ref, watch, onMounted } from "vue";
import { appInfo, loginSwitch } from "@/services/user";
import { appCategory, brand } from "@/services/goods";
import Index from "../home/index.vue";

import { plan } from "@/services/stocktaking";
// import { stockCounterStore } from "@/stores/stock";

// const stockObj = stockCounterStore();

const form = ref({});
const list = ref([]);
const showModal = ref(false);
const id = ref("");
const title = ref("");

onLoad((e) => {
  id.value = e.id ? e.id : "";

  title.value = e.name
    ? decodeURIComponent(e.name).slice(0, 10) +
      (decodeURIComponent(e.name).length > 10 ? "..." : "")
    : "";
  const topHeight = ref(uni.getStorageSync("topHight") || 0);
  code.value = e.code;
  // getList();
  getAppCategory();
});

const newCategoryList = ref([]);

const getAppCategory = async () => {
  try {
    let res = await brand(form.value);

    res.data.datas.forEach((item, index) => {
      item.select = false;
    });

    newCategoryList.value = res.data.datas;
    // newCategoryList.value = [];
  } catch (e) {
    console.log(e);
  }
};

const code = ref("");
const introData = ref([]);
const edit = ref(false);
const editName = () => {
  edit.value = true;
};

const getList = async () => {
  try {
    // 发送请求
    let res = await appInfo({});
    if (res.code == 200) {
      introData.value = res.data.employees;
      if (res.data.employees <= 0) {
        uni.redirectTo({
          url: "/pages/cashier/sales_order/index",
        });
      }
    }
  } catch (e) {
    uni.showToast({
      title: e.info,
      icon: "none",
    });
  }
};
// const selectFun = async (item) => {
//   uni.navigateTo({
//     url: `/pages/cashier/sales_order/index?ids=${
//       item.shopId
//     }&shopName=${decodeURIComponent(item.shopName)}`,
//   });
// };

const goadd = () => {
  uni.navigateTo({
    url: "/pages/cashier/sales_order/index",
  });
};
const onClickLeft = () => history.back();
const onClickRight = () => showToast("按钮");

const themeVars = ref({
  navBarIconColor: "#000000",
  navBarTextColor: "#000000",
  navBarBackground: "transparent",
});
const searchFun = () => {
  getAppCategory(1);
};

const openFun = (itemO, index, index2) => {
  let isOpen = itemO.open;

  newCategoryList.value.forEach((item, in1) => {
    if (in1 != index) {
      item.open = false;
    }
  });
  itemO.open = !isOpen;
};

const selectAll = ref(false);

const selectAllFun = () => {
  backFun(newCategoryList.value);

  selectAll.value = !selectAll.value;
};
const backFun = (list) => {
  list.forEach((item) => {
    item.select = !selectAll.value;
    if (item.children) {
      backFun(item.children);
    }
  });
};
const selectFun = (itemO, index, index2, index3) => {
  let isOpen = itemO.select;

  itemO.select = !isOpen;

  if (itemO.select) {
    let arr2 = newCategoryList.value.filter((element) => {
      return element.select == false;
    });
    if (arr2.length <= 0) {
      selectAll.value = true;
    }
  } else {
    selectAll.value = false;
  }
};
//创建任务单
const goCerate = async () => {
  uni.redirectTo({ url: "/pages/stocktaking/goods_p/index?id=" + id.value });
};

const goHome = () => {
  showModal.value = false;
};
</script>

<template>
  <top-nav :title="title" :border="false" rType="save"></top-nav>

  <!-- <view class="top" :style="'top: calc( 88rpx + ' + topHeight + 'px);'">
    <view class="search-k">
      <up-search
        shape="square"
        placeholder="品牌名称"
        v-model="form.name"
        :showAction="false"
        @search="searchFun"
      ></up-search>
    </view>
  </view> -->
  <view class="title">
    <view class="left">
      <view class="left-bz">
        <view>1</view>
      </view>
      <view class="left-text1">盘点商品</view>
    </view>
    <view class="left-hx"></view>
    <view class="left">
      <view class="left-bz">
        <view class="bz2">2</view>
      </view>
      <view class="left-text">完成盘点</view>
    </view>
  </view>

  <view class="content-image">
    <image src="@/static/nodate/datas.png" mode="scaleToFill" />
    <view class="content-image-text">盘点商品并提交盘点结果后进入下一步</view>
  </view>

  <view class="goods_bt_to">
    <!-- <view class="bt_left" @click="selectAllFun">
        <view class="select" :class="selectAll ? 'select2' : ''">
          <image
            v-if="selectAll"
            src="@/static/images/select.png"
            mode="scaleToFill"
          />
        </view>
        <text>全选</text>
      </view> -->

    <up-button
      class="bt_right"
      @click="goCerate"
      type="primary"
      color="#1679FB"
      text="开始盘点"
    ></up-button>
  </view>

  <up-modal
    width="600rpx"
    :showCancelButton="true"
    cancelText="返回首页"
    confirmText="去盘点"
    :show="showModal"
    :title="'创建成功了，可以去盘点了'"
    @confirm="goStock"
    @cancel="goHome"
  ></up-modal>
</template>

<style lang="scss" scoped>
@import "index.scss";
</style>
